{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting ./train-images-idx3-ubyte.gz\n",
      "Extracting ./train-labels-idx1-ubyte.gz\n",
      "Extracting ./t10k-images-idx3-ubyte.gz\n",
      "Extracting ./t10k-labels-idx1-ubyte.gz\n"
     ]
    }
   ],
   "source": [
    "from tensorflow.examples.tutorials.mnist import input_data\n",
    "mnist = input_data.read_data_sets(\".\", one_hot=True)\n",
    "\n",
    "import tensorflow as tf\n",
    "\n",
    "# Parameters\n",
    "learning_rate = 0.001\n",
    "training_epochs = 30\n",
    "batch_size = 100\n",
    "display_step = 1\n",
    "\n",
    "# Network Parameters\n",
    "n_input = 784 # MNIST data input (img shape: 28*28)\n",
    "n_classes = 10 # MNIST total classes (0-9 digits)\n",
    "\n",
    "# tf Graph input\n",
    "x = tf.placeholder(\"float\", [None, n_input])\n",
    "y = tf.placeholder(\"float\", [None, n_classes])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "#pre-define the  \n",
    "def conv2d(x, W):\n",
    "  return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')\n",
    "\n",
    "def max_pool_2x2(x):\n",
    "  return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],\n",
    "                        strides=[1, 2, 2, 1], padding='SAME')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "def multilayer_perceptron(x, weights, biases):\n",
    "    #now, we want to change this to a CNN network\n",
    "\n",
    "    #first reshape the data to 4-D\n",
    "\n",
    "    x_image = tf.reshape(x, [-1,28,28,1])\n",
    "\n",
    "    #then apply cnn layers\n",
    "\n",
    "    h_conv1 = tf.nn.relu(conv2d(x_image, weights['conv1']) + biases['conv_b1'])\n",
    "    h_pool1 = max_pool_2x2(h_conv1)\n",
    "\n",
    "    h_conv2 = tf.nn.relu(conv2d(h_pool1, weights['conv2']) + biases['conv_b2'])\n",
    "    h_pool2 = max_pool_2x2(h_conv2)\n",
    "\n",
    "    h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])\n",
    "    h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, weights['fc1']) + biases['fc1_b'])\n",
    "\n",
    "\n",
    "    # Output layer with linear activation\n",
    "    out_layer = tf.matmul(h_fc1, weights['out']) + biases['out_b']\n",
    "    return out_layer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Store layers weight & biases\n",
    "weights = {\n",
    "    'conv1': tf.Variable(tf.random_normal([5, 5, 1, 32])),\n",
    "    'conv2': tf.Variable(tf.random_normal([5, 5, 32, 64])),\n",
    "    'fc1' : tf.Variable(tf.random_normal([7*7*64,256])),\n",
    "    'out': tf.Variable(tf.random_normal([256,n_classes]))\n",
    "}\n",
    "biases = {\n",
    "    'conv_b1': tf.Variable(tf.random_normal([32])),\n",
    "    'conv_b2': tf.Variable(tf.random_normal([64])),\n",
    "    'fc1_b': tf.Variable(tf.random_normal([256])),\n",
    "    'out_b': tf.Variable(tf.random_normal([n_classes]))\n",
    "}\n",
    "\n",
    "# Construct model\n",
    "pred = multilayer_perceptron(x, weights, biases)\n",
    "\n",
    "# Define loss and optimizer\n",
    "cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))\n",
    "optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)\n",
    "\n",
    "# Initializing the variables\n",
    "init = tf.global_variables_initializer()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('Epoch:', '0001', 'cost=', '2005.953651756')\n",
      "('Epoch:', '0002', 'cost=', '361.200756125')\n",
      "('Epoch:', '0003', 'cost=', '222.655593089')\n",
      "('Epoch:', '0004', 'cost=', '154.397716973')\n",
      "('Epoch:', '0005', 'cost=', '108.289408546')\n",
      "('Epoch:', '0006', 'cost=', '83.728486200')\n",
      "('Epoch:', '0007', 'cost=', '63.813128544')\n",
      "('Epoch:', '0008', 'cost=', '52.091127872')\n",
      "('Epoch:', '0009', 'cost=', '38.352929364')\n",
      "('Epoch:', '0010', 'cost=', '30.455494692')\n",
      "('Epoch:', '0011', 'cost=', '25.972187011')\n",
      "('Epoch:', '0012', 'cost=', '20.754565103')\n",
      "('Epoch:', '0013', 'cost=', '18.515140012')\n",
      "('Epoch:', '0014', 'cost=', '14.170893429')\n",
      "('Epoch:', '0015', 'cost=', '13.025495452')\n",
      "('Epoch:', '0016', 'cost=', '11.380087092')\n",
      "('Epoch:', '0017', 'cost=', '12.045677507')\n",
      "('Epoch:', '0018', 'cost=', '9.095552578')\n",
      "('Epoch:', '0019', 'cost=', '8.405252479')\n",
      "('Epoch:', '0020', 'cost=', '7.802369204')\n",
      "('Epoch:', '0021', 'cost=', '8.664561321')\n",
      "('Epoch:', '0022', 'cost=', '6.413273589')\n",
      "('Epoch:', '0023', 'cost=', '7.001173552')\n",
      "('Epoch:', '0024', 'cost=', '3.928643572')\n",
      "('Epoch:', '0025', 'cost=', '6.000280571')\n",
      "('Epoch:', '0026', 'cost=', '3.947065584')\n",
      "('Epoch:', '0027', 'cost=', '5.913655243')\n",
      "('Epoch:', '0028', 'cost=', '4.686071558')\n",
      "('Epoch:', '0029', 'cost=', '3.783876064')\n",
      "('Epoch:', '0030', 'cost=', '3.133972832')\n",
      "Optimization Finished!\n",
      "('Accuracy:', 0.98420006)\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# Launch the graph\n",
    "with tf.Session() as sess:\n",
    "    sess.run(init)\n",
    "\n",
    "    # Training cycle\n",
    "    for epoch in range(training_epochs):\n",
    "        avg_cost = 0.\n",
    "        total_batch = int(mnist.train.num_examples/batch_size)\n",
    "        # Loop over all batches\n",
    "        for i in range(total_batch):\n",
    "            batch_x, batch_y = mnist.train.next_batch(batch_size)\n",
    "            # Run optimization op (backprop) and cost op (to get loss value)\n",
    "            _, c = sess.run([optimizer, cost], feed_dict={x: batch_x,\n",
    "                                                          y: batch_y})\n",
    "            # Compute average loss\n",
    "            avg_cost += c / total_batch\n",
    "        # Display logs per epoch step\n",
    "        if epoch % display_step == 0:\n",
    "            print(\"Epoch:\", '%04d' % (epoch+1), \"cost=\", \\\n",
    "                \"{:.9f}\".format(avg_cost))\n",
    "    print(\"Optimization Finished!\")\n",
    "\n",
    "    # Test model\n",
    "    correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))\n",
    "    # Calculate accuracy\n",
    "    accuracy = tf.reduce_mean(tf.cast(correct_prediction, \"float\"))\n",
    "    print(\"Accuracy:\", accuracy.eval({x: mnist.test.images, y: mnist.test.labels}))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
